<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Goods_course extends MY_Controller {
	function __construct(){
			parent::__construct();
			$this->load->model('Goods_courses');
		}
	/**
	 * 视频课
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	function index($search = 0, $cate_id=0, $kemu=0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post('category');
			$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			$kemu = (isset($post_data['kemu'])) ? $post_data['kemu'] : 0;
			$cate_id = (isset($post_data['cate_id'])) ? $post_data['cate_id'] : 0;
		}else{
			$search = urldecode($search);
			$page_config['uri_segment'] = 9;
		}
		$this->load->model('Courses');
		$page_config['uri_segment'] = 3;
		$offset = 10;
		$cate = $this->Courses->get_cate();
		$data = array('cate_id' => $cate_id, 'kemu'=>$kemu, 'search' => $search,'limit' => $limit);
		$subject = $this->Courses->get_course_cate_subject($cate_id);
		$result = $this->Goods_courses->get_list_ofpage($limit, $offset,$data);
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->load->library('pagination');
		foreach ($result['result'] as $key => $value) {
			$use_code = $this->Goods_courses->get_code($value['id'],1);
			$result['result'][$key]['use_code'] = $use_code;
			$nouse_code = $this->Goods_courses->get_code($value['id'],2);
			$result['result'][$key]['nouse_code'] = $nouse_code;
		}
		$page_config['base_url'] = '/goods_course/index/';
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $offset;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("cate", $cate);
		$this->smarty->assign("data", $data);
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("action", "录播课授权");
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("result", $result['result']);
		$this->smarty->display("goods_course/list.html"); 
	}

	function add($id){
		$post_data = $this->input->post();
		if(empty($post_data)){
			$school = $this->Goods_courses->get_school();
			$this->smarty->assign("post_url", "/goods_course/add/".$id);
			$this->smarty->assign("school", $school);
			$this->smarty->assign("action", "录播课授权");
			$this->smarty->display("goods_course/add.html"); 
		}else{
			if($post_data['num'] > 0){
				
				$school_info = get_field_by_where("o2o_school", "agent_id", "id='{$post_data['school_id']}'");
				if(!empty($school_info['agent_id'])){
					$agent_info = get_field_by_where("agents", "points", "id='{$school_info['agent_id']}'");
					if(!empty($agent_info['points'])){
						$post_data['agent_points'] = $agent_info['points'];
						$post_data['commission'] = sprintf("%.2f", ($post_data['num'] * $post_data['price']) * 22 / 100);
					}
				}
				$post_data['agent_id'] = $school_info['agent_id'];
				$post_data['data_id'] = $id;
				$post_data['data_type'] = 1;
				$post_data['type'] = 1;
				$post_data['add_time'] = time();
				$this->db->insert('o2o_school_order_records',$post_data);
				$record_id = $this->db->insert_id();

				$num = $post_data['num'];
				$time = time();
				for ($i=0; $i < $num; $i++) { 
					$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
					$code = $yCode[rand(0, 9)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
					$code_data[] = array("school_id" => $post_data['school_id'], "course_id" => $id, "add_time" => $time, "code" => "DV" . $code, "record_id" => $record_id, "expire_day" => $post_data['expire_day']);
				}
				$insert_result = $this->db->insert_batch("o2o_school_system_video_course_codes", $code_data);
			}else{
				echo "激活码数量不能为空！";exit;
			}
			/*if($insert_result){
				$post_data['data_id'] = $id;
				$post_data['data_type'] = 2;
				$post_data['type'] = 1;
				$post_data['add_time'] = time();
				$result = $this->db->insert('o2o_school_order_records',$post_data);
			}*/
			if($record_id > 0){
				show_message($_SERVER['HTTP_REFERER'],'添加成功');
			}else{
				show_message($_SERVER['HTTP_REFERER'],'操作失败');
			}
		}
	}


	function edit($id){
		$post_data = $this->input->post();
		$info = $this->Goods_courses->get_order_course_records($id);
		if(empty($info)){
			show_message("/goods_course", "数据不存在");exit;
		}
		if(isset($post_data['data_id']) && $post_data['data_id'] > 0 && isset($post_data['data_type']) && $post_data['data_type'] > 0 ){
			if($post_data['num'] > 0){
				$num = $post_data['num'];
				$time = time();
				if($post_data['data_type'] == 4){
					$category_id = 0;
					$subject_info = get_field_by_where("b_subject", "id, tk_category_id", "id='{$info['data_id']}'");
					if(empty($subject_info['id'])){
						show_message("/goods_course/edit/".$id, "科目不存在");exit;
					}else{
						if(!empty($subject_info['tk_category_id'])){
							$category_id = $subject_info['tk_category_id'];
						}
					}
				}
				for ($i=0; $i < $num; $i++) { 
					$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
					$code = $yCode[rand(0, 9)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
					if($post_data['data_type'] == 4){
						$code_data[] = array("school_id" => $info['school_id'], "category_id" => $category_id, "subject_id" => $info['data_id'], "add_time" => $time, "code" => "DV" . $code, 'record_id' => $id, "expire_day" => $post_data['expire_day']);
					}elseif($post_data['data_type'] == 1){
						$code_data[] = array("school_id" => $info['school_id'], "course_id" => $info['data_id'], "add_time" => $time, "code" => "DV" . $code, 'record_id' => $id, "expire_day" => $post_data['expire_day']);
					}elseif($post_data['data_type'] == 5){
						$code_data[] = array("school_id" => $info['school_id'], "sx_id" => $info['data_id'], "add_time" => $time, "code" => "DV" . $code, 'record_id' => $id, "expire_day" => $post_data['expire_day']);
					}
				}
				if($info['data_type'] == 4){
					$insert_result = $this->db->insert_batch("o2o_school_system_topic_codes", $code_data);
				}elseif($post_data['data_type'] == 1){
					$insert_result = $this->db->insert_batch("o2o_school_system_video_course_codes", $code_data);
				}
				elseif($post_data['data_type'] == 5){
					$insert_result = $this->db->insert_batch("o2o_school_system_shixun_codes", $code_data);
				}
			}else{
				show_message("/goods_course/edit/".$id, "激活码数量不能为空！");exit;
			}
			if($insert_result){
				$update_data['add_time'] = time();
				$update_data['reviewed'] = 1; 
				$update_data['price'] = $post_data['price']; 
				$update_data['reviewed'] = 1;
				$update_data['expire_day'] = $post_data['expire_day'];
				$this->db->where('id',$id);
				$result = $this->db->update('o2o_school_order_records',$update_data);
				if($result){
					if($info['data_type'] == 4){
						show_message('/order_course_auth/tiku_list','授权成功');
					}elseif($post_data['data_type'] == 1){
						show_message('/order_course_auth/index','授权成功');
					}else{
						show_message('/order_course_auth/shixun_list','授权成功');
					}
				}else{
					show_message($_SERVER['HTTP_REFERER'],'授权失败');
				}
			}
		}else{
			$school_info = get_field_by_where("o2o_school", "id, school_name", "id='{$info['school_id']}'");
			if($info['reviewed'] == 1){
				show_message("/goods_course", "已经授权过！");exit;
			}
			if($info['data_type'] == 4){
				$data_info = get_field_by_where("b_subject", "title", "id='{$info['data_id']}'");
				$this->smarty->assign("data_info", $data_info);
			}elseif($info['data_type'] == 1){
				$data_info = get_field_by_where("course", "title", "id='{$info['data_id']}'");
				$this->smarty->assign("data_info", $data_info);
			}elseif($info['data_type'] == 5){
				$data = json_decode(file_get_contents("http://123.206.137.187:804/HandlerData.ashx?action=GetZhangtaoInfo"),true);
				$data_info = array();
				if(!empty($data['rows']) && count($data['rows']) > 0){
					foreach ($data['rows'] as $key => $value) {
						if($value['Id'] == $info['data_id']){
							$data_info = $value;
							break;
						}
					}
				}
				$this->smarty->assign("data_info", array("title" => $data_info['ztName']));
			}
			if($info['data_type'] == 1){
				$ac = '录播课授权';
			}elseif($info['data_type'] == 4){
				$ac = '题库授权';
			}elseif($info['data_type'] == 5){
				$ac = '实训授权';
			}
			$this->smarty->assign("info", $info);
			$this->smarty->assign("school_info", $school_info);
			$this->smarty->assign("action", $ac);
			$this->smarty->display("goods_course/edit.html");
		}
		
	}
	
}
